function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

var Dug = {
	hover: false,
	curX: 0,
	curY: 0,
	debugger_X: 0,
	debugger_Y: 0,
	init: function(){
		// инициализация
		parent = this;
		// элементы
		$("body").prepend("<div class='dug_point'></div>");
		$("body").prepend("<div class='dug'><div class='dug_header'><span>Dug</span><div class='dug_close'></div></div><div class='dug_settings'></div></div>");
		$(".dug_settings").append("<p><label for='dugX'>X</label> <input type='text' style='width:50px' id='dugX' value='706' /></p>");
		$(".dug_settings").append("<p><input type='checkbox' id='dugHor' /> Horizontally</p>");
		$(".dug_settings").append("<p><input type='checkbox' id='dugVert' checked='checked' /> Vertically</p>");
		$(".dug_settings").append("<div class='dug_buttons'><input type='button' id='dugAddLine' value='add' /><input type='button' id='dugDelLines' value='delete' /><div>");
		// настройка
		parent.debugger_X = getCookie("debugger_X") || 0;
		parent.debugger_Y = getCookie("debugger_Y") || 0;
		show = getCookie("debugger_show") || "none";
		$(".dug").css({"top":parent.debugger_Y+"px","left":parent.debugger_X+"px","display":show});
		if (show == "block")
			$(".dug_point").css("display","none");
		
		// события
		$(".dug").children().not(".dug_header").mousedown(function(e){ e.stopPropagation() })	// остановка передачи события с элементов тела (кроме головы)
		$(".dug_close").mousedown(function(e){ e.stopPropagation() })							// и кнопки закрытия
		$("#dugAddLine").click(function(e){ parent.set(); })									// добавить линию
		$("#dugDelLines").click(function(e){ parent.del(); })									// удалить все линии
		$("#dugX").keypress(function(e){ if (e.keyCode == 13) parent.set(); })					// hotkey на Enter
		$(".dug_close").click(function(){ parent.hide(); });									// закрытие dug
		$(".dug_point").click(function(){ parent.show(); });									// открытие dug
		// события перещения
		$(".dug").mousedown(function(e){
			parent.hover = true;
			parent.curX = e.offsetX || e.layerX;
			parent.curY = e.offsetY || e.layerY;
		})
		$(document).mousemove(function(e){
			if (parent.hover)
				$(".dug").css({"top":e.pageY-parent.curY,"left":e.pageX-parent.curX,"opacity":"0.5"});
		})
		$(".dug").mouseup(function(){
			parent.hover = false;
			var left = $(this).offset().left,
			top = $(this).offset().top;
			$(this).css("opacity","1");
			setCookie("debugger_X",left,new Date("01 Jan 2015"),"/");
			setCookie("debugger_Y",top,new Date("01 Jan 2015"),"/");
			parent.debugger_X = left;
			parent.debugger_Y = top;
		})
		// события прокрутки окна
		$(window).scroll(function(){
			var wTop = $(window).scrollTop();
			if (wTop >= parent.debugger_Y)
				$(".dug").css({"top":wTop});
			else if (innerHeight + wTop <= parent.debugger_Y + $(".dug").height())
				$(".dug").css({"top": innerHeight + wTop - $(".dug").height()-2});
			else
				$(".dug").css({"top":parent.debugger_Y});
		})
	},
	set: function() {
		x = $("#dugX").val();
		if ($("#dugHor").attr('checked') == 'checked')
			$("body").prepend("<div class='dug_line' style='position:absolute; top:"+x+"px; width:"+$(document).width()+"px; height:1px; z-index:1000; background:red'></div>");
		if ($("#dugVert").attr('checked') == 'checked')
			$("body").prepend("<div class='dug_line' style='position:absolute; left:"+x+"px; width:1px; height:"+$(document).height()+"px; z-index:1000; background:red'></div>");
	},
	del: function() {
		$(".dug_line").remove();
	},
	show: function() {
		$(".dug_point").fadeOut();
		$(".dug").fadeIn();
		setCookie("debugger_show","block",new Date("01 Jan 2015"),"/");
	},
	hide: function() {
		$(".dug").fadeOut();
		$(".dug_point").fadeIn();
		setCookie("debugger_show","none",new Date("01 Jan 2015"),"/");
	}
};

$(document).ready(function(){
	Dug.init();
})